﻿Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class AddPicturesToAlbum
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Params("Album") = "" Then Response.Redirect("Default.aspx")
        'If the member is a "Tourist" then hide all controls for adding pictures 5-8 (only a "Greece Lover" can add 8 pictures to an album)
        If (Roles.IsUserInRole(User.Identity.Name, "Tourist")) Then
            f_Picture5.Enabled = False
            tb_Description5.Enabled = False
            tb_Picture5.Enabled = False
            RequiredFieldValidator5.Enabled = False
            f_Picture6.Enabled = False
            tb_Description6.Enabled = False
            tb_Picture6.Enabled = False
            RequiredFieldValidator6.Enabled = False
            f_Picture7.Enabled = False
            tb_Description7.Enabled = False
            tb_Picture7.Enabled = False
            RequiredFieldValidator7.Enabled = False
            f_Picture8.Enabled = False
            tb_Description8.Enabled = False
            tb_Picture8.Enabled = False
            RequiredFieldValidator8.Enabled = False
        End If
    End Sub

    Protected Sub btn_UploadPics_Click(sender As Object, e As EventArgs) Handles btn_UploadPics.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        'Create files for member if user is in role as "Greece Lover"
        Dim Pic5 As String = ""
        Dim Pic6 As String = ""
        Dim Pic7 As String = ""
        Dim Pic8 As String = ""
        'Save all filenames to disk
        Dim Pic1 As String = createUniqueFilename(IO.Path.GetExtension(f_Picture1.FileName), f_Picture1)
        Dim Pic2 As String = createUniqueFilename(IO.Path.GetExtension(f_Picture2.FileName), f_Picture2)
        Dim Pic3 As String = createUniqueFilename(IO.Path.GetExtension(f_Picture3.FileName), f_Picture3)
        Dim Pic4 As String = createUniqueFilename(IO.Path.GetExtension(f_Picture4.FileName), f_Picture4)
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            Pic5 = createUniqueFilename(IO.Path.GetExtension(f_Picture5.FileName), f_Picture5)
            Pic6 = createUniqueFilename(IO.Path.GetExtension(f_Picture6.FileName), f_Picture6)
            Pic7 = createUniqueFilename(IO.Path.GetExtension(f_Picture7.FileName), f_Picture7)
            Pic8 = createUniqueFilename(IO.Path.GetExtension(f_Picture8.FileName), f_Picture8)
        End If


        'Create an array in order to add all pictures in the database
        Dim AllUploadedPics(0 To 3) As String
        'Change size of array if member is a "Greece Lover"
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            Dim AllUploadedPics2(0 To 7) As String
            AllUploadedPics = AllUploadedPics2
        End If
        AllUploadedPics(0) = Pic1
        AllUploadedPics(1) = Pic2
        AllUploadedPics(2) = Pic3
        AllUploadedPics(3) = Pic4
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            AllUploadedPics(4) = Pic5
            AllUploadedPics(5) = Pic6
            AllUploadedPics(6) = Pic7
            AllUploadedPics(7) = Pic8
        End If

        'Create an array in order to add all description textbox's in the database
        Dim AllDescription(0 To 3) As String
        'Change size of array if member is a "Greece Lover"
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            Dim AllDescription2(0 To 7) As String
            AllDescription = AllDescription2
        End If
        AllDescription(0) = tb_Description1.Text
        AllDescription(1) = tb_Description2.Text
        AllDescription(2) = tb_Description3.Text
        AllDescription(3) = tb_Description4.Text
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            AllDescription(4) = tb_Description5.Text
            AllDescription(5) = tb_Description6.Text
            AllDescription(6) = tb_Description7.Text
            AllDescription(7) = tb_Description8.Text
        End If

        'Create an array in order to add all location textbox's in the database
        Dim AllLocation(0 To 3) As String
        'Change size of array if member is a "Greece Lover"
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            Dim AllLocation2(0 To 7) As String
            AllLocation = AllLocation2
        End If
        AllLocation(0) = tb_Picture1.Text
        AllLocation(1) = tb_Picture2.Text
        AllLocation(2) = tb_Picture3.Text
        AllLocation(3) = tb_Picture4.Text
        If Roles.IsUserInRole(User.Identity.Name, "Greece Lover") Then
            AllLocation(4) = tb_Picture5.Text
            AllLocation(5) = tb_Picture6.Text
            AllLocation(6) = tb_Picture7.Text
            AllLocation(7) = tb_Picture8.Text
        End If
        'Variable used to read all text from description and location text boxes which is saved to allLocation and allDescription arrays.
        Dim i As Integer = 0
        'Add files to database
        For Each newFileName As String In AllUploadedPics
            Dim SqlString As String = "INSERT INTO Album_Photos(Albums_FK, Picture, Thumbnail, Description, Location) VALUES (@f1, @f2, @f3, @f4, @f5)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("Album"))
            cmd.Parameters.AddWithValue("@f2", "Albums/" & newFileName)
            cmd.Parameters.AddWithValue("@f3", "Albums/Thumbnails/" & newFileName)
            cmd.Parameters.AddWithValue("@f4", AllDescription(i))
            cmd.Parameters.AddWithValue("@f5", AllLocation(i))
            cmd.ExecuteNonQuery()
            i = i + 1
        Next
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Albums.aspx")
    End Sub

    Protected Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        'Delete this album if user selects to cancel
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "DELETE FROM Albums WHERE ID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("Album"))
        cmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Albums.aspx")
    End Sub

    Private Function createUniqueFilename(ByVal FileNameStr As String, ByVal f_Picture As FileUpload) As String

        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() + FileNameStr

        If (f_Picture.HasFile And isPicture(FileNameStr)) Then
            Dim picLocationOnHardDiskServer = Request.MapPath("Albums") & "/" & newFileName
            f_Picture.SaveAs(picLocationOnHardDiskServer)

            'resizes an image to 100x100
            Dim img1 As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath("~/Albums/") + newFileName)
            Dim bmp1 As System.Drawing.Image = img1.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
            bmp1.Save(MapPath("~/Albums/Thumbnails/") + newFileName)

        Else
            Response.Redirect("Default.aspx")
        End If
        Return newFileName
    End Function

    'Check if picture uploaded is a valid picture.
    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".jpeg"
                Return True
            Case ".JPEG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function
End Class